package com.markerhub.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.dto.CollegeDto;
import com.markerhub.common.dto.MajorDto;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysCollege;
import com.markerhub.entity.SysCourse;
import com.markerhub.entity.SysMajor;
import com.markerhub.service.SysCollegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.markerhub.service.impl.SysCollegeServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2022-03-31
 */
@RestController
@RequestMapping("/sys/major")
public class SysMajorController extends BaseController {

    @Autowired
    SysCollegeService sysCollegeService;

    private static class medium{
        String college;
        String major;

        public String getCollege() {
            return college;
        }

        public String getMajor() {
            return major;
        }
    }

    @GetMapping("/list")//得到专业的列表
    public Result list(String username){
        List<SysMajor> pageData = sysMajorService.list(new QueryWrapper<SysMajor>()
                .like(StrUtil.isNotBlank(username), "username", username));
        return Result.succ(pageData);
    }

    @PostMapping("/listByCollege")//通过学院id得到专业的列表
    public Result listByCollege(@RequestBody SysCollege sysCollege){
        List<SysMajor> pageData = sysMajorService.list(new QueryWrapper<SysMajor>()
                .eq( "college_id", sysCollege.getId() ));
        return Result.succ(pageData);
    }

    @PostMapping("/listByCollegeName")//通过学院名字得到专业的列表
    public Result listByCollegeName(@RequestBody SysCollege sysCollege){
        List<SysMajor> pageData = sysMajorService.list(new QueryWrapper<SysMajor>()
                .eq( "college_id", sysCollegeService.getIdByCollege(sysCollege.getCollege()) ));
        return Result.succ(pageData);
    }

    @PostMapping("/save")//管理员录入专业信息
    public Result save(@Validated @RequestBody medium m) {
        SysMajor sys=null;
        sys=sysMajorService.getObject(m.getMajor());
        if(sys!=null){
            return Result.fail("专业已存在");
        }else{
            SysMajor sysMajor=new SysMajor();
            sysMajor.setCollegeId(sysCollegeService.getIdByCollege(m.getCollege()));
            sysMajor.setMajor(m.getMajor());
            sysMajorService.save(sysMajor);
            return Result.succ(sysMajor);
        }

    }

    @PostMapping("/delete")//删除专业信息
    public Result delete(@RequestBody SysMajor sysMajor){

        Long id=sysMajorService.getIdByMajor(sysMajor.getMajor());
        sysMajorService.removeById(id);

        return Result.succ("");

    }
    @PostMapping("/update")//修改专业的名字
    public Result update(@Validated @RequestBody MajorDto majorDto){
        SysMajor sys=null;
        sys=sysMajorService.getObject(majorDto.getNewName());
        if(sys!=null){
            return Result.fail("专业名重复");
        }else{
            SysMajor sysMajor=sysMajorService.getByMajorNme(majorDto.getOldName());
            sysMajor.setMajor(majorDto.getNewName());
            sysMajorService.updateById(sysMajor);
            return Result.succ("");
        }

    }

}
